package com.dm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dm.client.RestDmImageClient;
import com.dm.client.RestDmUserClient;
import com.dm.common.*;
import com.dm.pojo.DmImage;
import com.dm.pojo.DmUser;
import com.dm.service.LocalUserService;
import com.dm.vo.DmUserVO;
import com.dm.vo.TokenVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LocalUserServiceImpl implements LocalUserService {
    @Resource
    private RestDmUserClient restDmUserClient;
    @Resource
    private RestDmImageClient restDmImageClient;
    @Resource
    private RedisUtils redisUtils;

    @Override
    public Dto userLogin(String phone, String password) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("phone", phone);
        param.put("password", password);
        List<DmUser> dmUsersList = restDmUserClient.getDmUserListByMap(param);
        if (EmptyUtils.isEmpty(dmUsersList)) {
            return DtoUtil.returnFail("用户名或密码错误", "1006");
        } else {
            DmUser dmUser = dmUsersList.get(0);
            String userJson = (String) redisUtils.get(Constants.USER_TOKEN_PREFIX + dmUser.getId());
            if (EmptyUtils.isEmpty(userJson)) {
                Map<String, Object> imgParam = new HashMap<>();
                imgParam.put("targetId", dmUser.getId());
                imgParam.put("category", Constants.Image.ImageCategory.user);
                DmImage dmImage = restDmImageClient.getDmImageListByMap(imgParam).get(0);
                DmUserVO dmUserVo = new DmUserVO();
                BeanUtils.copyProperties(dmUser, dmUserVo);
                dmUserVo.setUserId(dmUser.getId());
                dmUserVo.setImageId(dmImage.getId());
                dmUserVo.setImgUrl(dmImage.getImgUrl());
                TokenVO tokenVO = createToken(dmUser);
                Object[] dtoObject = {dmUserVo, tokenVO};
                String userVoJson = JSONObject.toJSONString(dtoObject);
                redisUtils.set(dmUser.getId().toString(), Constants.Redis_Expire.SESSION_TIMEOUT, userVoJson);
                redisUtils.set(Constants.USER_TOKEN_PREFIX + dmUser.getId(), Constants.Redis_Expire.SESSION_TIMEOUT, tokenVO.getToken());
                return DtoUtil.returnDataSuccess(dtoObject);
            } else {
                return DtoUtil.returnFail("用户登录！！！", "0001");
            }
        }
    }

    @Override
    public Dto outUser(String token) throws Exception {
        String UserJson=(String)redisUtils.get(token);
        if (EmptyUtils.isEmpty(UserJson)){
            return DtoUtil.returnFail("用户信息已过期","1005");
        }else{
            redisUtils.delete(token);
            String message="用户已退出";
            return DtoUtil.returnDataSuccess(message);
        }
    }

    private TokenVO createToken(DmUser dmUser) {
        String token = "DM-" + MD5.getMd5(dmUser.getPhone() + dmUser.getId(), 16);
        TokenVO tokenVO = new TokenVO();
        tokenVO.setToken(token);
        tokenVO.setExpTime(Constants.Redis_Expire.SESSION_TIMEOUT);
        tokenVO.setGenTime(new Date().getTime());
        return tokenVO;
    }


}
